package cn.wp.common.core.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 框架拦截器
 * Created by chenyb on 2019-09-23
 */
public class WpInterceptorAdaptor implements HandlerInterceptor {

    private WpAbstractInterceptor abstractInterceptor;

    public WpInterceptorAdaptor(WpAbstractInterceptor abstractInterceptor) {
        this.abstractInterceptor = abstractInterceptor;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(abstractInterceptor.isSkip(request,response,handler)) {
            return true;
        }
        return abstractInterceptor.preHandle(request,response,handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if(abstractInterceptor.isEnabled(request,response,handler)){
            abstractInterceptor.handle(request,response,handler,modelAndView);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if(abstractInterceptor.isEnabled(request,response,handler)){
            abstractInterceptor.finalHandle(request,response,handler,ex);
        }
    }
}